#!/usr/bin/perl -w
#   Written by Jimmy Saw - 10-11-2007
#   This program takes a Genbank file and extract coordinates of genes 
#   You have to redirect output to a file

use Bio::SeqIO;

my $seqio_obj = Bio::SeqIO->new (-file => $ARGV[0], -format => 'genbank');
my $seq_obj;
my @seq_array;

while ($seq_obj = $seqio_obj->next_seq()){
    push (@seq_array, $seq_obj);
}

my $total = 0;
my $countseq = 0;

foreach $seq_obj (@seq_array){

    my $count = 0;

    for my $feat_obj ($seq_obj->get_SeqFeatures){

#        $count++;

        if ($feat_obj->primary_tag eq "CDS"){

            $count++;

            if ($feat_obj->has_tag('product')){
                for my $value ($feat_obj->get_tag_values('product')){
                    print $seq_obj->display_name(), "\t", $seq_obj->display_name(), "_", $count, "\t", $feat_obj->start, "\t", $feat_obj->end, "\t", $feat_obj->strand, "\t", "CDS", "\t", $value, "\n";
                }
            }
            elsif (!$feat_obj->has_tag('product')){
                print $seq_obj->display_name(), "\t", $seq_obj->display_name(), "_", $count, "\t", $feat_obj->start, "\t", $feat_obj->end, "\t", $feat_obj->strand, "\t", "CDS", "\t", "No definition", "\n";
            }
        }

        if ($feat_obj->primary_tag eq "tRNA"){

            $count++;

            if ($feat_obj->has_tag('product')){
                for my $value ($feat_obj->get_tag_values('product')){
                    print $seq_obj->display_name(), "\t", $seq_obj->display_name(), "_", $count, "\t", $feat_obj->start, "\t", $feat_obj->end, "\t", $feat_obj->strand, "\t", "tRNA", "\t", $value, "\n";
                }
            }
        }

        if ($feat_obj->primary_tag eq "rRNA"){

            $count++;

            if ($feat_obj->has_tag('product')){
                for my $value ($feat_obj->get_tag_values('product')){
                    print $seq_obj->display_name(), "\t", $seq_obj->display_name(), "_", $count, "\t", $feat_obj->start, "\t", $feat_obj->end, "\t", $feat_obj->strand, "\t", "rRNA", "\t", $value, "\n";
                }
            }
        }
    }
}

